<?php

include_once 'Expression.php';
include_once 'BinaryOperator.php';

/**
 * Represents an expression that consists of a binary operation between two expressions.
 * 
 * @package UC_PHPCodeModel
 * @author Uros Calakovic
 * @example ../Examples/BinaryOperatorExpressionExample.php 
 */

class BinaryOperatorExpression extends Expression
{
    /**
     * BinaryOperatorExpression constructor
     * 
     * @param BinaryOperator $operator One of the BinaryOperator constants
     * @param Expression $left The expression on the left of the operator
     * @param Expression $right The expression on the right of the operator
     */
    
    public function __construct(
        $operator, Expression $left = null, Expression $right = null)
    {
        parent::__construct();
        
        $left == null ? 
            $this->Left = new Expression() : 
            $this->set_Left($left);
        
        $right == null ? 
            $this->Right = new Expression() : 
            $this->set_Right($right);
        
        $this->set_Operator($operator);
    }
    
    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @param mixed $value The value the field should be set to, usually a collection.
     * @see CodeObject::__set()
     */
    
    public function __set($name, $value)
    {
        parent::__set($name, $value);
        
        if($name == 'Left')
        {
            $this->set_Left($value);
        }
        
        if($name == 'Right')
        {
            $this->set_Right($value);
        }
        
        if($name == 'Operator')
        {
            $this->set_Operator($value);
        }
    }

    /**
     * Used for making private / protected variables 
     * accessible from outside the class.
     * 
     * @param string $name Name of the field to be accessed
     * @return mixed The field that $name specified, usually a collection
     * @see CodeObject::__get()
     */
    
    public function __get($name)
    {
        $ret = parent::__get($name);
        
        if($ret != null)
        {
            return $ret;
        }
        
        if($name == 'Left')
        {
            return $this->get_Left();
        }
        
        if($name == 'Right')
        {
            return $this->get_Right();
        }
        
        if($name == 'Operator')
        {
            return $this->get_Operator();
        }
    }
    
    /**
     * The $Left getter method
     * @return Expression 
     */
    
    public function get_Left()
    {
        return $this->Left;
    }

    /**
     * The $Left setter method
     * @param Expression $Left 
     * 
     * @assert (123) throws PHPUnit_Framework_Error
     * @assert ('name') throws PHPUnit_Framework_Error
     */
    
    public function set_Left(Expression $Left)
    {
        $this->Left = $Left;
    }

    /**
     * The $Right getter method
     * @return Expression 
     */
    
    public function get_Right()
    {
        return $this->Right;
    }
    
    /**
     * The $Right setter method
     * @param Expression $Right 
     * 
     * @assert (123) throws PHPUnit_Framework_Error
     * @assert ('name') throws PHPUnit_Framework_Error
     */

    public function set_Right(Expression $Right)
    {
        $this->Right = $Right;
    }

    /**
     * The $Operator getter method
     * @return BinaryOperator 
     */
    
    public function get_Operator()
    {
        return $this->Operator;
    }

    /**
     * The $Operator setter method
     * @param type $Operator
     * @throws InvalidArgumentException If $Operator is not one of the BinaryOperator constants
     * 
     * @assert (123) throws InvalidArgumentException
     * @assert ('name') throws InvalidArgumentException
     */
    
    public function set_Operator($Operator)
    {
        if(is_string($Operator))
        {
            if(BinaryOperator::Defines($Operator))
            {
                $this->Operator = $Operator;
            }
            else
            {
                throw new InvalidArgumentException(
                    'The parameter must be a ' . 
                    'BinaryOperator class constant.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'The parameter must be a ' . 
                'BinaryOperator class constant.');
        }
    }

    /**
     * The expression on the left of the operator
     * @var Expression 
     */
    
    private $Left;
    
    /**
     * The expression on the right of the operator
     * @var Expression 
     */
    
    private $Right;
    
    /**
     * One of the BinaryOperator constants
     * @var BinaryOperator
     * @see BinaryOperator 
     */
    
    private $Operator;
}

?>
